{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Neural Networks Foundations  \n",
    "Implement Steps:  \n",
    "1. 激活函数  \n",
    "    sigmoid(x):Sigmoid激活函数，将输入映射到(0,1)之间  \n",
    "    sigmoid_dericative(x):Sigmoid 激活函数的导数，用于反向传播。  \n",
    "\n",
    "2. 输入数据和目标输出  \n",
    "    x: 输入数据，包含4个样本，每个样本有2个特征。  \n",
    "    y: 目标输出，表示逻辑与运算的结果。  \n",
    "\n",
    "3. 初始化权重与偏置\n",
    "    weights: 随机初始化权重矩阵,形状为(2,1)  \n",
    "    bias: 随机初始化的偏置,形状为(2,1)  \n",
    "\n",
    "4. 训练神经元  \n",
    "    前向传播: 计算输入数据通过神经元后的输出  \n",
    "    计算损失: 使用均方误差(MSE)作为损失函数  \n",
    "    反向传播: 计算损失相对于权重和偏置的梯度  \n",
    "    更新权重和偏置: 使用地图下降算法更新权重和偏置。  \n",
    "\n",
    "5. 测试神经元  \n",
    "    使用训练好的权重和偏置对心输入数据进行预测  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trained weights: [[4.64677161]\n",
      " [4.64677219]]\n",
      "Trained bias: [-7.06752957]\n",
      "Test output: [[0.90256139]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "# 激活函数\n",
    "def sigmoid(x):\n",
    "    return 1/(1+np.exp(-x))\n",
    "\n",
    "\n",
    "# 激活函数的导数\n",
    "def sigmoid_derivative(x):\n",
    "    return sigmoid(x)*(1-sigmoid(x))\n",
    "\n",
    "\n",
    "# 输入数据 (假设有2个特征)\n",
    "X=np.array([[0,0], [0,1], [1,0], [1,1]])\n",
    "# 目标输出(逻辑与运算)\n",
    "y=np.array([[0], [0], [0], [1]])\n",
    "\n",
    "# 初始化权重和偏置\n",
    "np.random.seed(42)\n",
    "weights=np.random.rand(2,1)\n",
    "bias=np.random.rand(1)\n",
    "\n",
    "# 学习率\n",
    "learning_rate=0.1\n",
    "# 训练次数\n",
    "n_iterationos=10000\n",
    "\n",
    "# 训练神经元\n",
    "for _ in range(n_iterationos):\n",
    "    # 前向传播\n",
    "    z=np.dot(X, weights)+bias\n",
    "    predictions=sigmoid(z)\n",
    "\n",
    "    # 计算损失\n",
    "    loss=np.mean((predictions-y)**2)\n",
    "\n",
    "    # 反向传播 \n",
    "    d_loss=2*(predictions-y)/y.size\n",
    "    d_predictions=sigmoid_derivative(z)\n",
    "    d_z=d_loss*d_predictions\n",
    "\n",
    "    # 更新权重和偏置\n",
    "    weights_gradient=np.dot(X.T, d_z)\n",
    "    bias_gradient=np.sum(d_z)\n",
    "\n",
    "    weights-=learning_rate*weights_gradient\n",
    "    bias-=learning_rate*bias_gradient\n",
    "\n",
    "print(f\"Trained weights: {weights}\")\n",
    "print(f\"Trained bias: {bias}\")\n",
    "\n",
    "# 测试神经元\n",
    "test_input=np.array([[1,1]])\n",
    "test_output=sigmoid(np.dot(test_input, weights)+bias)\n",
    "print(f\"Test output: {test_output}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm-learning",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
